#include "interface_win.h"

#include <QPainter>
#include <QApplication>
#include <QDebug>
#include "generatetools.h"

QString CODE_TXT = "https://kylinos.cn/?connectip=%1&version=%2&verify=ky";
const QString APP_CODE_TXT = "https://www.ubuntukylin.com/public/software/ky_mobile_assistant-1.0.9.apk";

InterfaceWin::InterfaceWin(QWidget *parent) : QWidget(parent)
{
    initWidget();
}

void InterfaceWin::setQRCode(QString codeInfo)
{
    if (codeInfo.isEmpty()) {
        QImage image(":/icon/defaultCode.png");

        QPainter painter(&image);
        painter.setPen(QPalette::Base);
        QFont font;
        font.setPointSize(12);
        painter.setFont(font);
        painter.drawText(image.rect(), Qt::AlignCenter, tr("No network detected"));

        m_pixmap = QPixmap::fromImage(image);
        m_qrCodeLabel->setPixmap(m_pixmap);
        m_qrCodeLabel->setAlignment(Qt::AlignCenter);
    } else {
        m_pixmap = GenerateTools::getCode(CODE_TXT.arg(codeInfo).arg(qApp->applicationVersion()));
        m_qrCodeLabel->setPixmap(m_pixmap);
    }
}

void InterfaceWin::setOnConnection(bool isConnection)
{
    QPixmap pixmap = m_pixmap;
    if (isConnection) {
        pixmap = pixmap.scaled(QSize(170, 170), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        QPixmap playPixmap(":/icon/codemantle.png");
        playPixmap = playPixmap.scaled(QSize(170, 170), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        QPainter imagepainter(&pixmap);                                        //新建画板
        imagepainter.setCompositionMode(QPainter::CompositionMode_SourceOver); //设置重叠效果
        imagepainter.drawPixmap(0, 0, playPixmap);
        imagepainter.end();
    }
    m_qrCodeLabel->setPixmap(pixmap);
}

void InterfaceWin::initWidget()
{
    QVBoxLayout *mainLayout = new QVBoxLayout();

    QHBoxLayout *codeLayout = new QHBoxLayout();
    QHBoxLayout *scanQRCodeLayout = new QHBoxLayout();
    QHBoxLayout *illustrateLayout = new QHBoxLayout();

    //二维码
    m_qrCodeLabel = new QLabel(this);
    m_qrCodeLabel->setFixedSize(174, 174);
    m_qrCodeLabel->setStyleSheet("background-color:#FFFFFF;");
    //二维码扫描
    QLabel *scanQRCode = new QLabel(this);
    //说明
    m_illustrate = new QLabel(this);
    m_illustrate->setFixedWidth(640);
    m_illustrate->setAlignment(Qt::AlignCenter);
    m_illustrate->setWordWrap(true);

    setQRCode();

    scanQRCode->setText(tr("Use the mobile app to scan this code"));
    QPalette pal;
    pal.setColor(QPalette::Text, QColor("#8F9399"));
    scanQRCode->setPalette(pal);
    m_illustrate->setText(tr("Connect the mobile phone and computer to the same network,open the")
                          + "<a href=\"https://www.ubuntukylin.com/public/software/ky_mobile_assistant-1.0.9.apk\""
                            "style=\"color:#3790FA;text-decoration:none\">"
                          + tr("mobile phone app") + "</a>" + tr("and scan the QR code."));
    connect(m_illustrate, &QLabel::linkActivated, this, [=](const QString url) {
        m_downAppWin = new kdk::KDialog(this);
        m_downAppWin->setFixedSize(360, 360);
        m_downAppWin->setAutoFillBackground(true);
        m_downAppWin->setBackgroundRole(QPalette::Base);

        QLabel *codeLabel = new QLabel(m_downAppWin);
        QLabel *descLab1 = new QLabel(m_downAppWin);
        QLabel *descLab2 = new QLabel(m_downAppWin);

        codeLabel->setFixedSize(174, 174);
        codeLabel->setStyleSheet("background-color:#FFFFFF;");

        QPixmap pixmap = GenerateTools::getCode(APP_CODE_TXT);
        codeLabel->setPixmap(pixmap);
        codeLabel->setAlignment(Qt::AlignCenter);

        descLab1->setText(tr("Scan this code with your mobile browser"));
        descLab1->setMargin(0);
        QPalette pal;
        pal.setColor(QPalette::Text, QColor("#8F9399"));
        descLab1->setPalette(pal);
        descLab2->setText(tr("Please scan this QR code with your mobile browser to download the app"));
        descLab2->setMargin(0);
        descLab2->adjustSize();
        descLab2->setWordWrap(true);
        descLab2->setFixedWidth(280);
        descLab2->setAlignment(Qt::AlignCenter);

        QHBoxLayout *hlayout1 = new QHBoxLayout;
        hlayout1->addStretch();
        hlayout1->addWidget(codeLabel);
        hlayout1->addStretch();
        hlayout1->setMargin(0);

        QHBoxLayout *hlayout2 = new QHBoxLayout;
        hlayout2->addStretch();
        hlayout2->addWidget(descLab1);
        hlayout2->addStretch();
        hlayout2->setMargin(0);

        QHBoxLayout *hlayout3 = new QHBoxLayout;
        hlayout3->addStretch();
        hlayout3->addWidget(descLab2);
        hlayout3->addStretch();
        hlayout3->setMargin(0);

        QVBoxLayout *layout = new QVBoxLayout;
        layout->setSpacing(0);
        layout->addSpacing(21);
        layout->addLayout(hlayout1);
        layout->addSpacing(6);
        layout->addLayout(hlayout2);
        layout->addSpacing(16);
        layout->addLayout(hlayout3);
        layout->addStretch();
        layout->setMargin(0);

        m_downAppWin->mainWidget()->setLayout(layout);
        m_downAppWin->setWindowModality(Qt::WindowModal);
        m_downAppWin->setWindowModality(Qt::ApplicationModal);
        m_downAppWin->setAttribute(Qt::WA_DeleteOnClose);
        m_downAppWin->show();
        m_downAppWin->exec();
    });

    codeLayout->addStretch();
    codeLayout->addWidget(m_qrCodeLabel);
    codeLayout->addStretch();

    scanQRCodeLayout->addStretch();
    scanQRCodeLayout->addWidget(scanQRCode);
    scanQRCodeLayout->addStretch();

    illustrateLayout->addStretch();
    illustrateLayout->addWidget(m_illustrate);
    illustrateLayout->addStretch();

    mainLayout->setSpacing(0);
    mainLayout->setMargin(0);
    mainLayout->addSpacing(136);
    mainLayout->addLayout(codeLayout);
    mainLayout->addSpacing(6);
    mainLayout->addLayout(scanQRCodeLayout);
    mainLayout->addSpacing(30);
    mainLayout->addLayout(illustrateLayout);
    mainLayout->addStretch();
    setLayout(mainLayout);
}
